//引入import
import Vue from "vue";
import Vuex from "vuex";
import counter from "./modules/counter";

//安装插件
Vue.use(Vuex);

//默认暴露

export default new Vuex.store({

  //汇总模块
  modules: {
    counter,
  },

  //属性
  state: {
    count: 0,
  },
  // 只读计算属性数据的容器
  getters: {
    oddOrEven(state) {
      return state.count % 2 === 1 ? "奇数" : "偶数";
    },
  },
 
  actions: {
    //+
    increment({ commit }, val) {
      //参数1 store  参数2 看dispatch方法是否传参
      commit("INCREMENT", val);
    },
    //-
    decrement({ commit, state }, val) {
      if (state.count % 2 === 1) {
        commit("INCREMENT", val);
      }
    },
    incrementIfOdd({ commit, state }, val) {
      if (state.count % 2 === 1) {
        commit("DECREMENT", val);
      }
    },
    incrementAsync({ commit }, val) {
      setTimeout(() => {
        commit("INCREMENT", val);
      }, 1000);
    },
  },
  mutations: {
    //state是默认参数   第二个参数看commit是否传递额外参数
    INCREMENT(state, val) {
      state.count += val;
    },
    DECREMENT(state, val) {
      state.count -= val;
    },
  },
});
